package demo11.gumballstatewinner;

import java.util.Random;
/**
 * 投入25分状态
 * */
public class HasQuarterState implements State {
	Random randomWinner = new Random(System.currentTimeMillis());//随机数（种子），系统当前时间System.currentTimeMillis()
	GumballMachine gumballMachine;//实例变量
 //通过构造函数得到糖果机引用并保存实例变量
	public HasQuarterState(GumballMachine gumballMachine) {
		this.gumballMachine = gumballMachine;
	}
  
	//投入25分状态的行为
	@Override
	public void insertQuarter() {
		System.out.println("你不能投入另一个25分");
	}
 
	@Override
	public void ejectQuarter() {
		System.out.println("已经退还25分");
		gumballMachine.setState(gumballMachine.getNoQuarterState());
	}
 
	@Override
	public void turnCrank() {
		System.out.println("得到糖果...");
		int winner = randomWinner.nextInt(10);//1-10
		if ((winner == 0) && (gumballMachine.getCount() > 1)) {
			gumballMachine.setState(gumballMachine.getWinnerState());
		} else {
			gumballMachine.setState(gumballMachine.getSoldState());
		}
	}

    @Override
	public void dispense() {
        System.out.println("没有糖果分配了");
    }
    
    @Override
	public void refill() { }
 
	@Override
	public String toString() {
		return "等待转动曲柄";
	}
}
